home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / hc2_x / tcprogud.sit / picture ƒ / cube.c < prev    next >
Text File  |  1991-02-16  |  4KB  |  179 lines

  1. /*
  2. *    FILE:        cube.c
  3. *    AUTHOR:        R. Gonzalez
  4. *    CREATED:    October 6, 1990
  5. *
  6. *    defines methods of Cube nested segment and Fast_Cube segment
  7. */
  8.  
  9. # include    "cube.h"
  10. # include    "line.h"
  11. # include    "trans.h"
  12.  
  13. /******************************************************************
  14. *    initialize cube
  15. ******************************************************************/
  16. boolean    Cube::init(void)
  17. {
  18.     int        i;
  19. /*    The following temp needed due to a bug in Think C dealing with
  20.     arrays of pointers, when the array is an instance variable.
  21. */
  22.     Segment    *temp_seg;
  23.     
  24.     Nested_Segment::init();
  25.     
  26.     num_segments = 12;
  27.     
  28.     for (i=0 ; i<12 ; i++)
  29.     {
  30.         temp_seg = new(Line);
  31.         segment[i] = temp_seg;
  32.         segment[i]->init();
  33.         segment[i]->set_color(WHITE);
  34.     }
  35.     
  36.     ((Line*) segment[0])->set_coord(0.,0.,0.,0.,0.,1.);
  37.     ((Line*) segment[1])->set_coord(0.,0.,1.,0.,1.,1.);
  38.     ((Line*) segment[2])->set_coord(0.,1.,1.,0.,1.,0.);
  39.     ((Line*) segment[3])->set_coord(0.,1.,0.,0.,0.,0.);
  40.     ((Line*) segment[4])->set_coord(1.,0.,0.,1.,0.,1.);
  41.     ((Line*) segment[5])->set_coord(1.,0.,1.,1.,1.,1.);
  42.     ((Line*) segment[6])->set_coord(1.,1.,1.,1.,1.,0.);
  43.     ((Line*) segment[7])->set_coord(1.,1.,0.,1.,0.,0.);
  44.     ((Line*) segment[8])->set_coord(0.,0.,0.,1.,0.,0.);
  45.     ((Line*) segment[9])->set_coord(0.,0.,1.,1.,0.,1.);
  46.     ((Line*) segment[10])->set_coord(0.,1.,1.,1.,1.,1.);
  47.     ((Line*) segment[11])->set_coord(0.,1.,0.,1.,1.,0.);
  48.     
  49.     return TRUE;
  50. }
  51.  
  52. /******************************************************************
  53. *    initialize fast cube
  54. ******************************************************************/
  55. boolean    Fast_Cube::init(void)
  56. {
  57.     int        i;
  58. /*    The following temp needed due to a bug in Think C dealing with
  59.     arrays of pointers, when the array is an instance variable.
  60. */
  61.     Coord3    *temp_coord;
  62.     
  63.     for (i=0 ; i<8 ; i++)
  64.     {
  65.         temp_coord = new(Coord3);
  66.         c[i] = temp_coord;
  67.         c[i]->init();
  68.     }
  69.     
  70.     c[0]->set(0.,0.,0.);
  71.     c[1]->set(0.,0.,1.);
  72.     c[2]->set(0.,1.,1.);
  73.     c[3]->set(0.,1.,0.);
  74.     c[4]->set(1.,0.,0.);
  75.     c[5]->set(1.,0.,1.);
  76.     c[6]->set(1.,1.,1.);
  77.     c[7]->set(1.,1.,0.);
  78.     
  79.     set_color(CYAN);
  80.     
  81.     return TRUE;
  82. }
  83.  
  84. /******************************************************************
  85. *    color fast cube
  86. ******************************************************************/
  87. void    Fast_Cube::set_color(color cube_color_val)
  88. {
  89.     cube_color = cube_color_val;
  90. }
  91.  
  92. /******************************************************************
  93. *    draw fast cube
  94. ******************************************************************/
  95. void    Fast_Cube::draw(Camera* camera,Projector* projector,
  96.                     Transformation* trans)
  97. {
  98.     int            i;
  99.     boolean        success = TRUE;
  100.     Coord2        *image[8];
  101.     Coord3        *new_coord;
  102.         
  103.     new_coord = new(Coord3);
  104.     new_coord->init();
  105.  
  106.     for (i=0 ; i<8 ; i++)
  107.     {
  108.         image[i] = new(Coord2);
  109.         image[i]->init();
  110.         new_coord->apply(c[i],trans);
  111.         if (!camera->take_photo(image[i],new_coord))
  112.             success = FALSE;
  113.     }
  114.     
  115.     if (success)
  116.     {    
  117.         projector->show_line(image[0],image[1],cube_color);
  118.         projector->show_line(image[1],image[2],cube_color);
  119.         projector->show_line(image[2],image[3],cube_color);
  120.         projector->show_line(image[3],image[0],cube_color);
  121.         projector->show_line(image[0],image[4],cube_color);
  122.         projector->show_line(image[4],image[5],cube_color);
  123.         projector->show_line(image[5],image[6],cube_color);
  124.         projector->show_line(image[6],image[7],cube_color);
  125.         projector->show_line(image[7],image[4],cube_color);
  126.         projector->show_line(image[1],image[5],cube_color);
  127.         projector->show_line(image[2],image[6],cube_color);
  128.         projector->show_line(image[3],image[7],cube_color);
  129.     }
  130.     
  131.     for (i=0 ; i<8 ; i++)
  132.     {
  133.         image[i]->destroy();
  134.         delete(image[i]);
  135.     }
  136.  
  137.     new_coord->destroy();
  138.     delete(new_coord);
  139. }
  140.  
  141. /******************************************************************
  142. *    move fast cube coordinates
  143. ******************************************************************/
  144. void    Fast_Cube::move(Transformation* trans)
  145. {
  146.     Coord3    *temp;
  147.     int        i;
  148.     
  149.     temp = new(Coord3);
  150.     temp->init();
  151.     
  152.     for (i=0 ; i<8 ; i++)
  153.     {
  154.         temp->equate(c[i]);
  155.         c[i]->apply(temp,trans);
  156.     }
  157.  
  158.     temp->destroy();
  159.     delete(temp);
  160. }
  161.  
  162. /******************************************************************
  163. *    destroy fast cube
  164. ******************************************************************/
  165. boolean    Fast_Cube::destroy(void)
  166. {
  167.     int        i;
  168.     
  169.     for (i=0 ; i<8 ; i++)
  170.     {
  171.         c[i]->destroy();
  172.         delete(c[i]);
  173.     }
  174.  
  175.     return TRUE;
  176. }
  177.  
  178.  
  179.